Skip to content

use getTerminatorOrNull for Windows EH with LLVM23#5121

Merged
thewilsonator merged 7 commits into
ldc-developers:masterfrom
gnavdev28:build_fix
May 6, 2026
Merged

use getTerminatorOrNull for Windows EH with LLVM23#5121
thewilsonator merged 7 commits into
ldc-developers:masterfrom
gnavdev28:build_fix

Conversation

@gnavdev28
Copy link
Copy Markdown
Contributor

@gnavdev28 gnavdev28 commented Apr 29, 2026

This PR contains two related fixes for building and running LDC with LLVM trunk on Windows:

1. MSVC Build Fix:

  • Fixed compilation errors in cl_options-llvm.cpp and llvmhelpers.cpp to ensure compatibility with LLVM trunk.

2. cloneBlocks Segfault Fix:

  • Fixed an 0xC0000005 Access Violation in cloneBlocks when compiling code with try-finally cleanups.
  • Added a defensive check term->isTerminator() in ms-cxx-helper.cpp to prevent crashing when the frontend generates a basic block without a proper terminator (e.g., ending abruptly with a call to __dtor).

Copilot AI review requested due to automatic review settings April 29, 2026 03:52
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR applies small source-level updates to restore/build compatibility with current LLVM APIs and tighten up a control-flow cloning edge case in the MS C++ EH helper code.

Changes:

  • Update cloneBlocks() to guard successor remapping using getNumSuccessors() before calling getSuccessor(0).
  • Switch label prefix emission to MCAsmInfo::getPrivateLabelPrefix() when printing inline-asm label names.
  • Update the opts::setFunctionAttributes() wrapper to match the newer llvm::codegen::setFunctionAttributes() parameter ordering/signature.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
gen/ms-cxx-helper.cpp Adds successor-count guarding when remapping the terminal branch target during block cloning.
gen/llvmhelpers.cpp Uses getPrivateLabelPrefix() for label-name emission.
driver/cl_options-llvm.cpp Updates wrapper call to llvm::codegen::setFunctionAttributes() to match the current signature.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread gen/ms-cxx-helper.cpp Outdated
Comment thread gen/ms-cxx-helper.cpp Outdated
Comment thread gen/ms-cxx-helper.cpp Outdated
Comment thread driver/cl_options-llvm.cpp Outdated
Comment thread gen/llvmhelpers.cpp
@thewilsonator thewilsonator changed the title Build fix Fix LLVM Error with Windows + LLVM23 + EH Apr 29, 2026
@thewilsonator thewilsonator requested a review from kinke April 29, 2026 05:57
@kinke
Copy link
Copy Markdown
Member

kinke commented May 1, 2026

Where did you hit this problem - when building druntime/Phobos already, or for an unrelated code base? In the first case, it might be an LLVM trunk regression/change (e.g., not returning null if there aren't any successors - which could still be fixed for LLVM 23, it wasn't even branched off yet). In the 2nd case, a testcase is needed.

@thewilsonator
Copy link
Copy Markdown
Contributor

When building druntime/phobos.

Alas I don't have a windows system, I'll ask on the LLVM discord and see what the consensus is.

@thewilsonator
Copy link
Copy Markdown
Contributor

getTerminator() now has a precondition that a terminator exists. You can use getTerminatorOrNull() if that doesn't hold

@kinke
Copy link
Copy Markdown
Member

kinke commented May 1, 2026

Oof; thx for asking. We have more usages incl. null checks.

@thewilsonator thewilsonator changed the title Fix LLVM Error with Windows + LLVM23 + EH use getTerminatorOrNull for Windows EH with LLVM23 May 3, 2026
Copy link
Copy Markdown
Contributor

@thewilsonator thewilsonator left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Otherwise looks great.

Comment thread gen/ms-cxx-helper.cpp Outdated
kinke
kinke previously requested changes May 3, 2026
Copy link
Copy Markdown
Member

@kinke kinke left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As said, there are more usages.

Comment thread gen/trycatchfinally.cpp Outdated
Comment on lines +334 to +338
#if LLVM_VERSION_MAJOR >= 23
return bb->getTerminatorOrNull();
#else
return bb->getTerminator();
#endif
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this one is not needed to be updated because it is in the #else branch of #if LLVM_VERSION_MAJOR >= 19

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tks for clarifying

@thewilsonator thewilsonator dismissed kinke’s stale review May 5, 2026 06:52

All other usages updated, except for the one in the else branch of #if LLVM_VERSION_MAJPR >= 19

@thewilsonator thewilsonator merged commit 79976be into ldc-developers:master May 6, 2026
20 of 21 checks passed
#else
auto *Term = B->getTerminator();
#endif
unsigned SuccCount = Term->getNumSuccessors();
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here (and in other sites) the breaking change for the existing API would have actually made sense, because we don't check for null afterwards, assuming that LLVM trunk introduced an appropriate assertion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants